<?php
/**
 * Invoke a child controller corresponding to a URL parameter.
 *
 * @author     Sam Eubank <sam@piephp.com>
 * @package    PiePHP
 * @since      Version 0.0
 * @copyright  Copyright (c) 2010, Pie Software Foundation
 * @license    http://www.piephp.com/license
 */

abstract class ParentController extends Controller {

	/**
	 * Invoke a child controller.
	 */
	public function catchAllAction() {
		global $PARAMETERS;

		// If this is the StoreController for example, its child controllers
		// will be in the Store directory.
		$directoryName = get_class($this);
		if ( substr($directoryName, -10) == 'Controller') 
		{
			// Slice "Controller" off the end of the controller name.
			$directoryName = substr($directoryName, 0, -10);
		}

		$controller = $directoryName . '_' . upper_camel($PARAMETERS[0]) . 'Controller';

		// If the URL was "/" or if it was "/hello/" and there's no
		// HelloController, we'll just use the DefaultController.  In the
		// "/hello/" case, there's a chace that DefaultController has a
		// helloAction or a catchAllAction.
		if (!class_exists($controller, true)) {
			$controller = 'DefaultController';
		}
		else {
			array_shift($PARAMETERS);
		}
		$controller = new $controller();
		
		$actionName = Pie::buildActionName($controller);

		call_user_func_array(array(&$controller, $actionName), $PARAMETERS);
	} // catchAllAction() 

} // class ParentController
